package org.sky.auto.basetools;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.sky.auto.driver.Browser;
import org.sky.auto.driver.DriverHandler;
import org.sky.auto.element.Locator;
import org.sky.auto.element.SElement;
import org.sky.auto.xml.XMLParser;


public class AutoBase {
	static Actions action;
	/**打开浏览器,里面包含了初始化操作，必须以此方法为开始*/
	public static void open(Browser browser,String url){
		DriverHandler.getInstance().getBrowserDriver(browser).get(url);
	}
	/**在本页面跳转到指定的链接处*/
	public static void open(String url){
		driver().get(url);
	}
	
	/**将鼠标移动到指定的元素位置上面*/
	public static void moveToElement(WebElement element){
		Actions action = new Actions((WebDriver) driver());
		action.moveToElement(element).build().perform();
	}

	/**通过这个方法可以得到driver的对象*/
	public static WebDriver driver(){
		return (WebDriver) DriverHandler.getInstance().getDriver().getEngine();
	}
	
	/**判断元素是否存在*/
	public static boolean isElementExist(By selector){
		try{
			driver().findElement(selector);
			//System.out.println("元素存在！");
		}catch(NoSuchElementException e){
			return false;
		}
		return true;
	}
	
	/**设置默认的元素等待时间*/
	public static void setElementWaitTime(long seconds){
		driver().manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
	}

	/**直接赋予一个select标签随机查找元素的方法，并且能够打印出索引值和此处的值*/
	public static void selectByRandom(WebElement element){
		Select select = new Select(element);
		int max = select.getOptions().size();
		Random rd = new  Random();
		int random = rd.nextInt(max);
		select.selectByIndex(random);
		String value = select.getOptions().get(random).getText().toString();
		System.out.println("索引值为--->"+random);
		System.out.println("此索引值处的选项值为--->"+value);
	}
//	/**通过xml里面定义的元素id属性值获取元素*/
//	public static WebElement element(String id){
//		return XMLParser.element(id);
//	}
	/**层级定位元素*/
	public static WebElement element(String...ids){
		WebElement e=null;
		By by = null;
		int index = 0;
		int len =ids.length;
		Map<By,Integer> byMap;
		for(int i=0;i<len;i++){
			byMap=XMLParser.by(ids[i]);
			System.out.println(byMap.size());
			Set<Map.Entry<By, Integer>> setEntry = byMap.entrySet();
			Iterator<Map.Entry<By, Integer>> iter=setEntry.iterator();
			while(iter.hasNext()){
				Map.Entry<By,Integer> entry=(Map.Entry<By,Integer>)iter.next();
				by=entry.getKey();
				index=entry.getValue();
				break;
			}
			if(i==0){
				e=driver().findElements(by).get(index);		
				continue;
			}else{
				e=e.findElements(by).get(index);
				continue;
			}
		}
		return e;
		
	}
	/**基础方法，得到鼠标键盘事件基础*/
	public static Actions getActions(){
		if(action==null){
			return new Actions(driver());
		}
		return action;
	}
	
	/**按下指定键操作*/
	public static void keyDown(Keys key){
		getActions().keyDown(key).build().perform();
	}
	/**在某个元素上面进行指定键的按下操作*/
	public static void keyDown(WebElement element,Keys key){
		getActions().keyDown(element, key).build().perform();
	}
	/**点击某个元素*/
	public static void click(WebElement element){
		getActions().click(element);
	}
	/**拖拽某个元素*/
	public static void dragAndDrop(WebElement fromElement,WebElement toElement){
		getActions().dragAndDrop(fromElement,toElement).build().perform();
	}
	/**拖拽某个元素到指定的位置*/
	public static void dragAndDrop(WebElement fromElement,int xlocation, int ylocation){
		getActions().dragAndDropBy(fromElement, xlocation, ylocation).build().perform();
	}
	/**将按下的键松开*/
	public static void keyUp(Keys key){
		getActions().keyUp(key);
	}
	/**在指定的元素上面进行按键松开*/
	public static void keyUp(WebElement element,Keys key){
		getActions().keyUp(element, key);
	}
	/**获取元素的焦点，主要是输入框的焦点*/
	public static void focus(WebElement element){
		getActions().sendKeys(element,"");
	}
	/**按下鼠标左键在指定的元素位置上*/
	public static void leftMouseDown(WebElement element){
		getActions().clickAndHold(element);
	}
	/**松开在指定元素位置的鼠标左键*/
	public static void leftMouseUp(WebElement onElement){
		getActions().release(onElement);
	}
	/**在指定元素的位置双击操作*/
	public static void doubleClick(WebElement element){
		getActions().doubleClick(element);
	}
	/**在指定的元素处输入*/
	public static void sendKeys(WebElement element,java.lang.CharSequence...charSequences){
		getActions().sendKeys(element, charSequences);
	}
	/**关闭所有的窗口*/
	public static void closeAllWindow(){
		driver().quit();
	}
	/**关闭当前的窗口*/
	public static void closeCurrentWindow(){
		driver().close();
	}
//	/**通过id得到SElement的元素进行操作*/
//	public static SElement sElement(String id){
//		return XMLParser.sElement(id);
//	}
	/**支持SElement元素的层级定位方式*/
	public static SElement sElement(String...ids){
		int len = ids.length;
		SElement e = new SElement();
		By by = null;
		int index = 0;
		for(int i=0;i<len;i++){
			Map<By,Integer> byMap=XMLParser.by(ids[i]);
			Set<Map.Entry<By, Integer>> setEntry = byMap.entrySet();
			Iterator<Map.Entry<By, Integer>> iter=setEntry.iterator();
			while(iter.hasNext()){
				Map.Entry<By,Integer> entry=(Map.Entry<By,Integer>)iter.next();
				by=entry.getKey();
				index=entry.getValue();
				break;
			}
			if(i==0){
				try {
					e.addLocator(by,index);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				e=e.childElement(by, index);
			}
		}
		return e;
	}
	/**通过id的方式来判断元素是否存在*/
	public static boolean isElementExist(String id){
		By by=null;
		int index=0;
		Map<By,Integer> byMap=XMLParser.by(id);
		Set<Map.Entry<By, Integer>> setEntry = byMap.entrySet();
		Iterator<Map.Entry<By, Integer>> iter=setEntry.iterator();
		while(iter.hasNext()){
			Map.Entry<By,Integer> entry=(Map.Entry<By,Integer>)iter.next();
			by=entry.getKey();
			index=entry.getValue();
			break;
		}
		try{
			driver().findElements(by).get(index);
		}catch(NoSuchElementException e){
			return false;
		}
		return true;
	}
	/**SElement的选择定位器
	 * @throws Exception */
	public SElement locator(String by,String selectorValue,int index) throws Exception{
		SElement e= new SElement();
		if(by.equals("id")){
			e.addLocator(Locator.Id,selectorValue,index);
		}else if(by.equals("css")){
			e.addLocator(Locator.Css, selectorValue,index);
		}else if(by.equals("tagName")){
			e.addLocator(Locator.TagName, selectorValue,index);
		}else if(by.equals("class")){
			e.addLocator(Locator.Class, selectorValue,index);
		}else if(by.equals("xpath")){
			e.addLocator(Locator.Xpath, selectorValue,index);
		}else if(by.equals("linkText")){
			e.addLocator(Locator.LinkText, selectorValue,index);
		}else if(by.equals("name")){
			e.addLocator(Locator.Name, selectorValue,index);
		}else if(by.equals("partialLinkText")){
			e.addLocator(Locator.PartialLinkText,selectorValue,index);
		}else{
			System.out.println("没有找到相应的定位方式");
		}
		return e;
	}
	/**SElement的选择定位器
	 * @throws Exception */
	public SElement locator(String by,String selectorValue) throws Exception{
		SElement e= new SElement();
		if(by.equals("id")){
			e.addLocator(Locator.Id,selectorValue);
		}else if(by.equals("css")){
			e.addLocator(Locator.Css, selectorValue);
		}else if(by.equals("tagName")){
			e.addLocator(Locator.TagName, selectorValue);
		}else if(by.equals("class")){
			e.addLocator(Locator.Class, selectorValue);
		}else if(by.equals("xpath")){
			e.addLocator(Locator.Xpath, selectorValue);
		}else if(by.equals("linkText")){
			e.addLocator(Locator.LinkText, selectorValue);
		}else if(by.equals("name")){
			e.addLocator(Locator.Name, selectorValue);
		}else if(by.equals("partialLinkText")){
			e.addLocator(Locator.PartialLinkText,selectorValue);
		}else{
			System.out.println("没有找到相应的定位方式");
		}
		return e;
	}
	
	public WebElement selector(String by,String selector,int index){
		WebElement element=null;
		if(by.equals("id")){
			element = AutoBase.driver().findElements(By.id(selector)).get(index);
		}else if(by.equals("css")){
			element = AutoBase.driver().findElements(By.cssSelector(selector)).get(index);
		}else if(by.equals("tagName")){
			element = AutoBase.driver().findElements(By.tagName(selector)).get(index);
		}else if(by.equals("class")){
			element = AutoBase.driver().findElements(By.className(selector)).get(index);
		}else if(by.equals("xpath")){
			element = AutoBase.driver().findElements(By.xpath(selector)).get(index);
		}else if(by.equals("linkText")){
			element = AutoBase.driver().findElements(By.linkText(selector)).get(index);
		}else if(by.equals("name")){
			element = AutoBase.driver().findElements(By.name(selector)).get(index);
		}else if(by.equals("partialLinkText")){
			element = AutoBase.driver().findElements(By.partialLinkText(selector)).get(index);
		}else{
			System.out.println("没有找到相应的定位方式");
		}
		return element;
	}
	
	/**通过索引值获得元素对象*/
	public WebElement selector(String by,String selector){
		WebElement element=null;
		if(by.equals("id")){
			element = AutoBase.driver().findElement(By.id(selector));
		}else if(by.equals("css")){
			element = AutoBase.driver().findElement(By.cssSelector(selector));
		}else if(by.equals("tagName")){
			element = AutoBase.driver().findElement(By.tagName(selector));
		}else if(by.equals("class")){
			element = AutoBase.driver().findElement(By.className(selector));
		}else if(by.equals("xpath")){
			element = AutoBase.driver().findElement(By.xpath(selector));
		}else if(by.equals("linkText")){
			element = AutoBase.driver().findElement(By.linkText(selector));
		}else if(by.equals("name")){
			element = AutoBase.driver().findElement(By.name(selector));
		}else if(by.equals("partialLinkText")){
			element = AutoBase.driver().findElement(By.partialLinkText(selector));
		}else{
			System.out.println("没有找到相应的定位方式");
		}
		return element;
	}
	
	public static WebElement elemement(String id){
		WebElement e=null;
		By by = null;
		int index=0;
		Map<By,Integer> byMap;
		byMap=XMLParser.by(id);
		//System.out.println(byMap.size());
		Set<Map.Entry<By, Integer>> setEntry = byMap.entrySet();
		Iterator<Map.Entry<By, Integer>> iter=setEntry.iterator();
		while(iter.hasNext()){
			Map.Entry<By,Integer> entry=(Map.Entry<By,Integer>)iter.next();
			by=entry.getKey();
			index=entry.getValue();
			break;
		}
		e=driver().findElements(by).get(index);		
		return e;
	}
	
	public static SElement sElements(String id){
		SElement e = new SElement();
		By by = null;
		int index = 0;
		Map<By,Integer> byMap=XMLParser.by(id);
		Set<Map.Entry<By, Integer>> setEntry = byMap.entrySet();
		Iterator<Map.Entry<By, Integer>> iter=setEntry.iterator();
		while(iter.hasNext()){
			Map.Entry<By,Integer> entry=(Map.Entry<By,Integer>)iter.next();
			by=entry.getKey();
			index=entry.getValue();
			break;
		}
		try {
			e.addLocator(by,index);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		return e;
	}
}
